package com.cyan.note.controller;

import com.cyan.note.common.ApiResult;
import com.cyan.note.entity.Shop;
import com.cyan.note.mapper.ShopMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 店铺功能测试控制器
 * 用于测试Shop实体和ShopMapper的基础功能
 * 
 * @author ZhangzhenYu
 * @since 2025-01-14
 */
@RestController
@RequestMapping("/test/shop")
public class ShopTestController {

    @Autowired
    private ShopMapper shopMapper;

    /**
     * 测试查询所有店铺基础信息
     */
    @GetMapping("/list")
    public ApiResult<List<Shop>> getAllShops() {
        try {
            List<Shop> shops = shopMapper.selectAllBasicInfo();
            return ApiResult.success("查询店铺列表成功", shops);
        } catch (Exception e) {
            return ApiResult.error("查询店铺列表失败: " + e.getMessage());
        }
    }

    /**
     * 测试根据ID查询店铺
     */
    @GetMapping("/{id}")
    public ApiResult<Shop> getShopById(@PathVariable Long id) {
        try {
            Shop shop = shopMapper.selectById(id);
            if (shop != null) {
                return ApiResult.success("查询店铺成功", shop);
            } else {
                return ApiResult.error("店铺不存在");
            }
        } catch (Exception e) {
            return ApiResult.error("查询店铺失败: " + e.getMessage());
        }
    }

    /**
     * 测试按状态查询店铺
     */
    @GetMapping("/status/{status}")
    public ApiResult<List<Shop>> getShopsByStatus(@PathVariable String status) {
        try {
            List<Shop> shops = shopMapper.selectByStatus(status);
            return ApiResult.success("按状态查询店铺成功", shops);
        } catch (Exception e) {
            return ApiResult.error("按状态查询店铺失败: " + e.getMessage());
        }
    }

    /**
     * 测试统计功能
     */
    @GetMapping("/stats")
    public ApiResult<?> getShopStats() {
        try {
            long totalCount = shopMapper.countTotal();
            long activeCount = shopMapper.countByStatus("active");
            long suspendedCount = shopMapper.countByStatus("suspended");
            long closedCount = shopMapper.countByStatus("closed");
            
            return ApiResult.success("统计数据获取成功", java.util.Map.of(
                "totalCount", totalCount,
                "activeCount", activeCount,
                "suspendedCount", suspendedCount,
                "closedCount", closedCount
            ));
        } catch (Exception e) {
            return ApiResult.error("获取统计数据失败: " + e.getMessage());
        }
    }

    /**
     * 测试创建店铺（仅用于测试）
     */
    @PostMapping("/create-test")
    public ApiResult<Shop> createTestShop() {
        try {
            Shop shop = new Shop();
            shop.setName("测试店铺" + System.currentTimeMillis());
            shop.setCode("TEST" + System.currentTimeMillis());
            shop.setDescription("这是一个测试店铺");
            shop.setContactPhone("400-000-0000");
            shop.setStatus("active");
            shop.setCreatedBy(1L); // 超级管理员创建
            
            int result = shopMapper.insert(shop);
            if (result > 0) {
                return ApiResult.success("创建测试店铺成功", shop);
            } else {
                return ApiResult.error("创建测试店铺失败");
            }
        } catch (Exception e) {
            return ApiResult.error("创建测试店铺失败: " + e.getMessage());
        }
    }

    /**
     * 测试更新店铺状态
     */
    @PutMapping("/{id}/status/{status}")
    public ApiResult<String> updateShopStatus(@PathVariable Long id, @PathVariable String status) {
        try {
            int result = shopMapper.updateStatus(id, status);
            if (result > 0) {
                return ApiResult.success("店铺状态已更新为: " + status);
            } else {
                return ApiResult.error("状态更新失败，店铺不存在");
            }
        } catch (Exception e) {
            return ApiResult.error("状态更新失败: " + e.getMessage());
        }
    }

    /**
     * 测试检查店铺编号是否存在
     */
    @GetMapping("/check-code/{code}")
    public ApiResult<?> checkShopCode(@PathVariable String code) {
        try {
            long count = shopMapper.countByCode(code);
            return ApiResult.success("店铺编号检查完成", java.util.Map.of(
                "code", code,
                "exists", count > 0,
                "count", count
            ));
        } catch (Exception e) {
            return ApiResult.error("店铺编号检查失败: " + e.getMessage());
        }
    }
}